package com.clx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clx.constants.SystemConstants;
import com.clx.domain.ResponseResult;
import com.clx.domain.entity.Comment;
import com.clx.domain.vo.CommentVo;
import com.clx.domain.vo.PageVo;
import com.clx.enums.AppHttpCodeEnum;
import com.clx.exception.SystemException;
import com.clx.mapper.CommentMapper;
import com.clx.service.CommentService;
import com.clx.service.UserService;
import com.clx.utils.BeanCopyUtils;
import com.clx.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2025-04-08 22:21:55
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    UserService userService;

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        // 查询对应文章的根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT_TYPE.equals(commentType), Comment::getArticleId, articleId);
        queryWrapper.eq(Comment::getRootId, SystemConstants.DEFAULT_COMMENT_ROOT_ID);
        queryWrapper.eq(Comment::getType, commentType);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        // articleId + rootId == -1 + 分页
        Page<Comment> page = new Page(pageNum, pageSize);
        page(page, queryWrapper);

        List<CommentVo> commentVos = toCommentVoList(page.getRecords());
        // 查询所有根评论对应的子评论几何，并赋值给对应的属性
        for (CommentVo commentVo : commentVos) {
            // 查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            // 赋值
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVos, page.getTotal()));
    }

    @Override
    public ResponseResult addComent(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())) {
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId, id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        return toCommentVoList(comments);

    }

    private List<CommentVo> toCommentVoList(List<Comment> list) {
        List<CommentVo> commentVos =  BeanCopyUtils.copyBeanList(list, CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            // 通过createBy查询用户昵称
            commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getNickName());;

            if (commentVo.getToCommentId() != -1) {
                commentVo.setToCommentUserName(userService.getById(commentVo.getToCommentUserId()).getNickName());
            }
            // 通过toCommentUserId 查询评论用户名称
        }
        return commentVos;
    }
}

